package cn.dankal.flyaudio.store;

import android.view.View;

import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.EvaluateImgAdapter;
import cn.dankal.flyaudio.adapter.EvaluateListAdapter;
import cn.dankal.flyaudio.contract.EvaluateListContract;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;
import cn.dankal.flyaudio.presenter.EvaluateListPresenter;
import cn.dankal.flyaudio.view.StarView;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 28/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.store
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class EvaluateListActivity extends BaseActivity implements View.OnClickListener,
        EvaluateListContract.View {
    private EvaluateListPresenter commentsDetailsPresenter;
    String id;
    EvaluateListAdapter commentsListAdapter;
    AppCompatImageView ivAvatar, ivLike,ivAwesome, ivMessage;
    AppCompatTextView itemTvName, itemTvTime, itemTvContent, tvLikeNum;
    private StarView ratingBar;
    private AppCompatEditText editContent;
    private CommentsEntity commentsEntity;
    private EvaluateImgAdapter evaluateImgAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_evaluate_list;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        commentsDetailsPresenter.detachView();
    }

    @Override
    protected void initComponents() {
        commentsDetailsPresenter = new EvaluateListPresenter();
        commentsDetailsPresenter.attachView(this);
        RecyclerView recyclerView = findViewById(R.id.mRv1);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        commentsListAdapter = new EvaluateListAdapter();
        recyclerView.setAdapter(commentsListAdapter);
        id = getIntent().getStringExtra("uuid");
        commentsDetailsPresenter.getEvaluate(id);
        itemTvName = findViewById(R.id.item_tv_name);
        RecyclerView itemRvImg = findViewById(R.id.item_rv_img);

        itemRvImg.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL,
                false));
         evaluateImgAdapter = new EvaluateImgAdapter();
        itemRvImg.setAdapter(evaluateImgAdapter);
        ratingBar = findViewById(R.id.ratingBar);
        ivAvatar = findViewById(R.id.item_iv_avatar);
        itemTvTime = findViewById(R.id.item_tv_time);
        itemTvContent = findViewById(R.id.item_tv_content);
        tvLikeNum = findViewById(R.id.tv_like_num);
        ivLike = findViewById(R.id.iv_like);

        editContent = findViewById(R.id.edit_content);
        ivAwesome = findViewById(R.id.iv_awesome);
        ivAwesome.setOnClickListener(this);
        ivMessage = findViewById(R.id.iv_message);
        ivMessage.setOnClickListener(this);
        ivLike.setOnClickListener(this);
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        LiveEventBus.get("EvaluateRefresh", String.class).observe(this,
                new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        commentsDetailsPresenter.getEvaluate(id);
                    }
                });
    }

    @Override
    public void evaluateSuccess(CommentsEntity commentsEntity) {
        this.commentsEntity = commentsEntity;
        if (commentsEntity.getAppUser() != null) {
            PicUtils.loadAvatar(commentsEntity.getAppUser().getAvatar(), ivAvatar);
            itemTvName.setText(commentsEntity.getAppUser().getUsername());
        }
        itemTvName.setTextColor(getResources().getColor(R.color.color_00BBDB));
        itemTvTime.setText(commentsEntity.getCreatedDatetime());
        itemTvContent.setText(commentsEntity.getContent());
        tvLikeNum.setText("已有" + commentsEntity.getLikeCount() + "人点赞");
        setLikeRes();

        if(commentsEntity.getImgList()!=null){
            evaluateImgAdapter.addDatas(commentsEntity.getImgList(),true);
        }
        switch (commentsEntity.getScoreType()) {
            case "POOR":
                ratingBar.setMark(1f);
                break;
            case "AVERAGE":
                ratingBar.setMark(2f);
                break;
            case "GOOD":
                ratingBar.setMark(3f);
                break;
        }
        commentsListAdapter.addDatas(commentsEntity.getChildren(), true);
    }

    private void setLikeRes() {
        if ("true".equals(commentsEntity.getiLike())) {
            ivLike.setImageResource(R.mipmap.ic_awesome);
            ivAwesome.setImageResource(R.mipmap.ic_awesome);
        } else {
            ivLike.setImageResource(R.mipmap.ic_awesome_un);
            ivAwesome.setImageResource(R.mipmap.ic_awesome_un);
        }
    }

    @Override
    public void likeSuccess() {
        commentsEntity.setiLike("true");
        setLikeRes();
    }

    @Override
    public void unLikeSuccess() {
        commentsEntity.setiLike("false");
        setLikeRes();
    }

    @Override
    public void sendSuccess() {
        commentsDetailsPresenter.getEvaluate(id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_message:
                if (StringUtil.isEmpty(editContent.getText().toString())) {
                    ToastUtils.showShort("请输入评价内容");
                    return;
                }
                commentsDetailsPresenter.sendMessage(commentsEntity.getUuid(),
                        editContent.getText().toString());
                break;
            case R.id.iv_like:
            case R.id.iv_awesome:
                if ("true".equals(commentsEntity.getiLike())) {
                    commentsDetailsPresenter.unLike(id);
                } else {
                    commentsDetailsPresenter.like(id);
                }
                break;
        }
    }
}
